package com.mianle.userapi.util;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.UUID;

public class Tools {

    /**
     * 生成token
     *
     * @return String
     */
    public static String GetGUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }

    /**
     * 获取当前Unix时间戳(毫秒)
     *
     * @return curTime
     */
    public static long getCurUnixTime() {
        long curTime = System.currentTimeMillis();
        return curTime;
    }

    /**
     * 获取当前用户信息
     *
     * @return CurrentAdminInfo
     */
//    public static CurrentUserInfo getUserInfo(HttpServletRequest request) {
//        CurrentUserInfo currentUserInfo = new CurrentUserInfo();
//        Map<String, Object> claims = (Map<String, Object>) request.getAttribute("jwt_info");
//        int userID = claims.containsKey("user_id") ? (int) claims.get("user_id") : 0;
//        String userName = claims.containsKey("user_name") ? (String) claims.get("user_name") : "";
//        String openid = claims.containsKey("openid") ? (String) claims.get("openid") : "";
//        currentUserInfo.setUserId(userID);
//        currentUserInfo.setUserName(userName);
//        currentUserInfo.setWxOpenid(openid);
//        return currentUserInfo;
//    }


    //判断 是否是微信浏览器
    public static boolean isWx(HttpServletRequest request) {
        String userAgent = request.getHeader("user-agent").toLowerCase();
        return userAgent.indexOf("micromessenger") > -1 ? true : false;//微信客户端
    }


    /**
     * 生成随机数
     *
     * @param num 位数
     * @return
     */
    public static String createRandomNum(int num) {
        String randomNumStr = "";
        for (int i = 0; i < num; i++) {
            int randomNum = (int) (Math.random() * 10);
            randomNumStr += randomNum;
        }
        return randomNumStr;
    }
}
